<?php echo $this->element('displayHeader'); ?>
<?php echo $ajax->form('SDRH', 'post', array('update'=>'content', 'indicator' => 'indicator', 'url' => array('action' => 'save')));?>
<div class="toolbar">
    <div id="tLeft">
        <?php if ($mode == 'add') : ?>
            <?php echo $this->element('toolbar', array('buttons' => array('save', 'clear', 'back'))); ?>
        <?php else :  // mode edit ?>
            <?php echo $this->element('toolbar', array('buttons' => array('add', 'save', array('print' => array('model' => 'SDRH')), 'back'))); ?>
        <?php endif; ?>
    </div>
    <div id="tRight"><?php echo $this->element('neighbors', array('model' => 'SDRH', 'title' => 'id')); ?></div>
</div>

<?php echo $form->input('SDRH.id'); ?>
<?php echo $form->hidden('mode', array('value' => $mode)); ?>

<?php if ($mode == 'add' && $dispatches == null) : ?>
<script type="text/javascript">
    $j(function() {
        alert("There is no Dispatch to returned");
        $j('.save').hide();
    });
</script>
<?php endif; ?>
<?php if ($mode == 'add' && !empty($this->data['SDH'])) {
    echo $form->hidden('dispatch_status_id', array('value' => $this->data['SDH']['dispatch_status_id']));
} ?>
<fieldset>
    <table>
        <tr>
            <td width="180px"><?php echo $form->label('SDRH.return_no', 'Return no'); ?></td>
            <td width="300px">
                <?php if ($mode == 'edit') : ?>
                <span id="return_no"><?php echo $this->data['SDRH']['return_no'] ?></span>
                <?php endif; ?>
                &nbsp;
            </td>
            <td width="180px"><?php echo $form->label('SDRH.supplier_dispatch_head_id', 'Dispatch no.', array('class' => 'required')); ?></td>
            <td>
                <?php if ($mode == 'add') : ?>
                    <?php echo $form->select('SDRH.supplier_dispatch_head_id', $dispatches, $this->data['SDH']['id'], array('tabindex' => 5)); ?>
                <?php else : ?>
                <span id="dispatch_no"><?php echo $this->data['SDH']['dispatch_no'] ?></span>
                <?php endif; ?>
            </td>
        </tr>
        <tr>
            <td><?php echo $form->label('SDRH.return_date', 'Return date'); ?></td>
            <td>
                <?php if ($mode == 'add') : ?>
                    <?php echo $html->tag('span', $today); ?>
                    <?php echo $form->hidden('SDRH.return_date', array('id' => 'return_date', 'value' => $today)); ?>
                <?php else : ?>
                <span id="return_date"><?php echo $this->data['SDRH']['return_date'] ?></span>
                <?php endif; ?>
            </td>
            <td><?php echo $form->label('SDRH.customer_id', 'Customer'); ?></td>
            <td>
                <?php if ($mode == 'add') : ?>
                    <?php echo $form->hidden('SDRH.customer_id', array('value' => $this->data['SDH']['customer_id'])); ?>
                <span id="SDRHCustomerName"><?php if (!empty($this->data)) echo $this->data['Customer']['name']; ?></span>
                <?php else : ?>
                    <?php echo $this->data['Customer']['name'] ?>
                <?php endif; ?>
            </td>
        </tr>
        <tr>
            <td><?php echo $form->label('SDRH.return_by', 'Return by'); ?></td>
            <td>
                <?php if($mode == 'add') : ?>
                    <?php echo $html->tag('span', $return_by); ?>
                    <?php echo $form->hidden('SDRH.return_by', array('value' => $return_by)); ?>
                <?php else : ?>
                    <?php echo $html->tag('span', $this->data['SDRH']['return_by']); ?>
                <?php endif; ?>
            </td>
            <td><?php echo $form->label('SDRH.site_id', 'Site'); ?></td>
            <td>
                <?php if ($mode == 'add') : ?>
                    <?php echo $form->hidden('SDRH.site_id', array('value' => $this->data['SDH']['site_id'])); ?>
                <span id="SDRHSiteName"><?php if (!empty($this->data)) echo $this->data['Site']['name']; ?></span>
                <?php else : ?>
                    <?php echo $this->data['Site']['name'] ?>
                <?php endif; ?>
            </td>
        </tr>
    </table>
</fieldset>

<fieldset>
    <table class="data">
        <thead>
            <tr>
                <th class="no">No</th>
                <th width="130px">Part no.</th>
                <th width="130px">Stock Code.</th>
                <th>Description</th>
                <?php if ($mode == 'add') : ?>
                <th width="100px">Deliver Qty</th>
                <?php endif; ?>
                <th width="100px">Return Qty</th>
                <th width="50px">Unit</th>
                <th width="100px">Price</th>
                <th width="100px">Amount</th>
            </tr>
        </thead>
        <?php if (empty($this->data)) : ?>
        <tbody id="productRow">
            <tr class="odd-cell">
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
        </tbody>
        <?php elseif ($mode == 'add') : $count = count($this->data['SD'])?>
        <tbody id="tbodySDR">
                <?php for($i = 0; $i < $count; ++$i) : ?>
            <tr class="<?php echo (($i + 1) % 2 == 0)? 'even-cell' : 'odd-cell'; ?>">
                <td><span><?php echo $i + 1; ?></span></td>
                <td>
                            <?php echo $form->input("SDR.{$i}.id"); ?>
                            <?php echo $form->hidden("SDR.{$i}.product_id", array('value' => $this->data['SD'][$i]['product_id'])); ?>
                            <?php echo $form->hidden("SDR.{$i}.cust_product_site_id", array('value' => $this->data['SD'][$i]['cust_product_site_id'])); ?>
                            <?php echo $form->hidden("SDR.{$i}.price", array('value' => $this->data['SD'][$i]['price'])); ?>
                            <?php echo $form->hidden("SDR.{$i}.return_qty", array('value' => $this->data['SD'][$i]['qty'] - $this->data['SD'][$i]['stock_in_qty'])); ?>
                            <?php echo $form->hidden("SDR.{$i}.supplier_dispatch_id", array('value' => $this->data['SD'][$i]['id'])); ?>
                            <?php echo $html->link($this->data['SD'][$i]['Product']['part_no'],
                            '/supplier_consignment_lists/view/' . $this->data['SD'][$i]['product_id'] . '/' . strtotime('now'),
                            array('target' => '_blank')); ?>
                </td>
                <td><?php echo $this->data['SD'][$i]['CustProductSite']['stock_code']; ?></td>
                <td><?php echo $this->data['SD'][$i]['Product']['text']; ?></td>
                <td class="text-right"><?php echo $this->data['SD'][$i]['qty']; ?></td>
                <td class="text-right"><?php echo $return_qty = $this->data['SD'][$i]['qty'] - $this->data['SD'][$i]['stock_in_qty']; ?></td>
                <td class="center"><?php echo $this->data['SD'][$i]['Product']['Unit']['title']; ?></td>
                <td class="text-right"><?php echo number_format($this->data['SD'][$i]['price'], 2); ?></td>
                <td class="text-right"><?php echo number_format($this->data['SD'][$i]['price'] * $return_qty, 2); ?></td>
            </tr>
                <?php endfor; ?>
        </tbody>
        <?php else : $count = count($this->data['SDR']) // mode edit?>
        <tbody>
                <?php for($i = 0; $i < $count; ++$i) : ?>
            <tr class="<?php echo (($i + 1) % 2 == 0)? 'even-cell' : 'odd-cell'; ?>">
                <td><span><?php echo $i + 1; ?></span></td>
                <td>
                            <?php echo $html->link($this->data['SDR'][$i]['Product']['supplier_part_no'],
                            '/supplier_consignment_lists/view/' . $this->data['SDR'][$i]['product_id'] . '/' . strtotime('now'),
                            array('target' => '_blank')); ?>
                </td>
                <td><?php echo $this->data['SDR'][$i]['CustProductSite']['stock_code']; ?></td>
                <td><?php echo $this->data['SDR'][$i]['Product']['title']; ?></td>
                <td class="text-right"><?php echo $this->data['SDR'][$i]['return_qty']; ?></td>
                <td class="center"><?php echo $this->data['SDR'][$i]['Product']['Unit']['title']; ?></td>
                <td class="text-right"><?php echo number_format($this->data['SDR'][$i]['price'], 2); ?></td>
                <td class="text-right"><?php echo number_format($this->data['SDR'][$i]['price'] * $this->data['SDR'][$i]['return_qty'], 2); ?></td>
            </tr>
                <?php endfor; ?>
        </tbody>
        <?php endif; ?>
    </table>
</fieldset>
<fieldset>
    <table>
        <tr>
            <td width="50px" class="top"><?php echo $form->label('SDRH.remark'); ?></td>
            <td><?php echo $form->textarea('SDRH.remark', array('cols' => 80, 'rows' => 3)); ?></td>
        </tr>
    </table>
</fieldset>
<?php echo $form->end(); ?>

<script type="text/javascript">
<?php if ($mode == 'add') : ?>
    $j(function() {
        $j('#SDRHSupplierDispatchHeadId').bind('change', function(event) {
            requestUpdater('content', 'supplier_dispatch_returns/view/supplier_dispatch_head_id:' + $j(this).val());
        });

        $j('#SDRHId').parent('form').beforeSubmit(function() {
            if ($j.areEmpty('SDRHSupplierDispatchHeadId')) {
                return false;
            }
        });
    });
<?php endif; ?>
</script>

<?php //debug($this->data); ?>